﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Xml;
using System.Xml.Serialization;
using DAL.Repositories;
using Model.Entities;
using Model.ViewModels;

namespace Web.Controllers
{
    public class XmlController : Controller
    {
        public ActionResult Search(string id, string areaId)
        {
            var searchRep = new SearchRepository();
            var apartments = searchRep.Search(id, areaId);

            var xmlString = ToXml(apartments);
            return Content(xmlString, "text/xml");
        }

        public ActionResult SearchByFloor(string id)
        {
            var seacrhRep = new SearchRepository();
            var apartments = seacrhRep.GetInfoByFloor(id);

            var xmlString = ToXml(apartments);
            return Content(xmlString, "text/xml");
        }

        public ActionResult SearchByKeyword(string id)
        {
            var searchRep = new SearchRepository();
            var apartments = searchRep.SearchByKeyword(id);

            var xmlString = ToXml(apartments);
            return Content(xmlString, "text/xml");
        }

        public ActionResult CodeList(string id)
        {
            switch (id.ToLower())
            {
                case "areaintervallist":
                    var areaIntervalRep = new IntervalRepository();
                    var areaIntervalsList = areaIntervalRep.ReadAllByCategory(IntervalCategory.Area);
                    var areaIntervalsXmlString = ToXml(areaIntervalsList);
                    return Content(areaIntervalsXmlString, "text/xml");
                case "floorlist":
                    var floors = Enum.GetNames(typeof (Floor));
                    var vmFloorList = floors.Select((t, i) => new CodeListVm()
                    {
                        Id = i, 
                        Name = t.Replace('_', ' ')
                    }).ToList();

                    var floorsXmlString = ToXml(vmFloorList);
                    return Content(floorsXmlString, "text/xml");
            }

            return null;
        }

        public ActionResult Apartment(int id)
        {
            var apartmentRep = new ApartmentRepository();
            var apartment = apartmentRep.ReadDetails(id);

            var xmlString = ToXml(apartment);

            //xml to insert (graphics node)
            var code = apartment.Code;
            
            var graphicXmlString = "<graphics>" +
                                       "<layout>www.ban-centar.hr/Content/Images/Layout/" + code + ".png</layout>" +
                                       "<layoutthumb>www.ban-centar.hr/Content/Images/Layout/Thumb/" + code + ".png</layoutthumb>" +
                                       "<topview>www.ban-centar.hr/Content/Images/TopView/" + code + ".png</topview>" +
                                       "<topviewthumb>www.ban-centar.hr/Content/Images/TopView/Thumb/" + code + ".png</topviewthumb>" +
                                       "<table>www.ban-centar.hr/Content/Images/Table/" + code + ".png</table>" +
                                   "</graphics>";

            //create xml document to append new node 
            var doc = new XmlDocument();
            doc.LoadXml(xmlString);

            //create xml fragment that will be append 
            var docFrag = doc.CreateDocumentFragment();
            docFrag.InnerXml = graphicXmlString;

            //append fragment 
            doc.DocumentElement.AppendChild(docFrag);

            //convert xml doc to xml string 
            var sw = new StringWriter();
            var tx = new XmlTextWriter(sw);
            doc.WriteTo(tx);

            xmlString = sw.ToString();

            return Content(xmlString, "text/xml");
        }

        public ActionResult GraphicsWithId()
        {
            var apartmentRep = new ApartmentRepository();
            var apartment = apartmentRep.ReadGraphics();

            var xmlString = ToXml(apartment);
            return Content(xmlString, "text/xml");
        }

        public static string ToXml(object entity)
        {
            var myNamespaces = new XmlSerializerNamespaces();
            myNamespaces.Add("xs", "http://www.w3.org/2001/XMLSchema");
            myNamespaces.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance");

            var xs = new XmlSerializer(entity.GetType());
            using (var memoryStream = new MemoryStream())
            {
                var settings = new XmlWriterSettings()
                {
                    Encoding = Encoding.UTF8
                };
                using (var writer = XmlWriter.Create(memoryStream, settings))
                {
                    xs.Serialize(writer, entity);
                }

                var xml = Encoding.UTF8.GetString(memoryStream.GetBuffer());
                xml = xml.Substring(xml.IndexOf(Convert.ToChar(60)));
                xml = xml.Substring(0, (xml.LastIndexOf(Convert.ToChar(62)) + 1));
                return xml;
            }
        }

    }
}
